/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.domain.user.service.impl;

import com.itgrail.pilot.domain.user.entity.DictDataE;
import com.itgrail.pilot.domain.user.entity.DictTypeE;
import com.itgrail.pilot.domain.user.repository.DictDataRepository;
import com.itgrail.pilot.domain.user.repository.DictTypeRepository;
import com.itgrail.pilot.domain.user.service.MetaDataService;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.ddd.annotation.domain.DomainService;
import com.itgrail.grail.exception.BusinessException;

@DomainService
public class MetaDataServiceImpl implements MetaDataService {

    @Autowired
    private DictTypeRepository dictTypeRepository;

    @Autowired
    private DictDataRepository dictDataRepository;


    @Override
    public void addDictType(DictTypeE dictTypeE) {
        DictTypeE dictTyp = dictTypeRepository.findByDictCode(dictTypeE.getDictCode());
        if (null != dictTyp) {
            throw new BusinessException("400", "数据字典Code:" + dictTyp.getDictCode() + "不能重复");
        }
        dictTypeE.setDictTypeRepository(dictTypeRepository);
        dictTypeE.save();
    }

    @Override
    public void updateDictType(DictTypeE dictTypeE) {
        dictTypeE.setDictTypeRepository(dictTypeRepository);
        dictTypeE.update();
    }

    @Override
    public void deleteDictType(Long id) {
        DictTypeE dictType = dictTypeRepository.findById(id);
        if (null == dictType) {
            throw new BusinessException("400", "删除的数据不存在!");
        }
        dictType.setDictTypeRepository(dictTypeRepository);
        dictType.delete();

    }

    @Override
    public void addDictData(DictDataE dictDataE) {
        DictDataE dictData = dictDataRepository.findDictDataByDictCodeAndItemCode(dictDataE.getDictCode(),
                dictDataE.getItemCode());
        if (null != dictData) {
            throw new BusinessException("400", "数据项Code:" + dictData.getItemCode() + "不能重复");
        }
        dictDataE.setDictDataRepository(dictDataRepository);
        dictDataE.save();
    }

    @Override
    public void updateDictData(DictDataE dictDataE) {
        dictDataE.setDictDataRepository(dictDataRepository);
        dictDataE.update();
    }

    @Override
    public void deleteDictData(Long id) {
        DictDataE dictDataE = dictDataRepository.findById(id);
        if (null == dictDataE) {
            throw new BusinessException("400", "删除的数据不存在!");
        }
        dictDataE.setDictDataRepository(dictDataRepository);
        dictDataE.delete();

    }
}
